<template>
  <el-dialog
    class="add-progress-out-model"
    title="进度产值设置"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(600)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        :model="ruleForm"
        :rules="rules"
        :label-width="$setRem(150)"
        ref="addProgressForm"
        class="rule-form">
        <el-form-item label="计划产值" prop="planOut" required>
          <el-input-number :controls="false" :min="0" placeholder="请输入计划产值" v-model="ruleForm.planOut"></el-input-number>
        </el-form-item>
        <el-form-item label="实际产值" prop="actualOut" required>
          <el-input-number :controls="false" :min="0" placeholder="请输入实际产值" v-model="ruleForm.actualOut"></el-input-number>
        </el-form-item>
        <el-form-item label="实际成本" prop="actualCost" required>
          <el-input-number :controls="false" :min="0" placeholder="请输入实际成本" v-model="ruleForm.actualCost"></el-input-number>
        </el-form-item>
        <el-form-item label="产值月份" prop="monthOut" required>
          <el-date-picker
            v-model="ruleForm.monthOut"
            format="yyyy-MM"
            value-format="yyyy-MM"
            type="month"
            placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入备注"
            v-model="ruleForm.remark">
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleCancel">取 消</el-button>
      <el-button
        class="success"
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        提 交
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      ruleForm: {
        planOut: undefined,
        actualOut: undefined,
        actualCost: undefined,
        monthOut: '',
        remark: '',
      },
      // 校验规则
      rules: {
        planOut: [
          { required: true, type: 'number', message: '请输入计划产值' },
          { trigger: 'blur', type: 'number' },
        ],
        actualOut: [
          { required: true, type: 'number', message: '请输入实际产值' },
          { trigger: 'blur', type: 'number' },
        ],
        actualCost: [
          { required: true, type: 'number', message: '请输入实际成本值' },
          { trigger: 'blur', type: 'number' },
        ],
        monthOut: [
          { required: true, message: '请选择产值月份' },
          { trigger: 'change' },
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (!val) {
        this.ruleForm = {
          planOut: undefined,
          actualOut: undefined,
          actualCost: undefined,
          monthOut: '',
          remark: '',
        };
      }
    },
    formData: {
      deep: true,
      handler(data) {
        if (data) {
          Object.keys(this.ruleForm).forEach(key => {
            this.ruleForm[key] = data[key];
          });
        }
      }
    }
  },
  methods: {
    // 取消
    handleCancel() {
      this.$emit('cancel', false);
    },
    handleSubmit() {
      this.$refs.addProgressForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/dialog';

  .add-progress-out-model {
    .form-container {
      /deep/ .el-form-item {
        .el-form-item__label {
          color: #fff;
        }
        .el-form-item__content {
          .el-input, .el-textarea {
            width: 340px;
          }
        }
      }
    }
  }
</style>
